package com.saas.controller;

import com.saas.pojo.Products;
import com.saas.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller     //获取页面时
@RestController     //URL增删改查时
public class ProductController {

    @Autowired
    IProductsService ips;

    @GetMapping("/getAllProducts")
    //页面
//    public String getAllProducts(HttpServletRequest request){
//        List<Products> products = ips.getAllProducts();
//        request.setAttribute("products",products);
//        return "products.jsp";
//    }
    //URL增删改查
    public List<Products> getAllProducts(){
        List<Products> products = ips.getAllProducts();
        return products;
    }

    @GetMapping("/GetProductByPid")
//    public String getProductById(HttpServletRequest request){
//        String id = request.getParameter("id");
//        Products product = ips.getProductById(id);
//        request.setAttribute("product",product);
//        return "product.jsp";
//    }
    public Products getProductById(@RequestParam Integer id){
        Products product = ips.getProductById(id);
        return product;
    }

    @PostMapping("/UpdateProduct")
//    public String updateProduct(Products product){
//        ips.updateProduct(product);
//        return "redirect:/Product";
//    }
    public boolean updateProduct(@RequestBody Products product){
        boolean flag = ips.updateProduct(product);
        return flag;
    }
    @GetMapping("/DeleteProduct")
//    public String deleteProduct(HttpServletRequest request){
//        String sid = request.getParameter("id");
//        int id = sid == null ? 0 : Integer.parseInt(sid);
//        ips.deleteProduct(id);
//        return "redirect:/Product";
//    }
    public boolean deleteProduct(@RequestParam Integer id){
        boolean flag = ips.deleteProduct(id);
        return flag;
    }
//    @GetMapping("/DeleteProductByPath/{id}")
//    public String deleteProductByPath(@PathVariable int id){
//        ips.deleteProduct(id);
//        return "redirect:/Product";
//    }

    @PostMapping("/SaveProduct")
    public boolean saveProduct(@RequestBody Products product){
        boolean flag = ips.saveProduct(product);
        return flag;
    }

}
